<?php
/**
 * User: xuhong
 * Date: 2018/7/6
 */

namespace extend\wechat\mp;

class Format
{
    /**
     * 回复 文本消息
     * @param $mainAttr
     * @return string
     */
    public static function text($mainAttr)
    {
        $framework = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";

        $resultStr = sprintf($framework, $mainAttr['fromUsername'], $mainAttr['toUsername'], $mainAttr['time'], 'text', $mainAttr['msg']);
        return $resultStr;
    }

    /**
     * 回复 图片消息
     * @param $mainAttr
     * @return string
     */
    public static function image($mainAttr)
    {
        $framework = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";

        $resultStr = sprintf($framework, $mainAttr['fromUsername'], $mainAttr['toUsername'], $mainAttr['time'], 'image', $mainAttr['mediaID']);
        return $resultStr;
    }

    /**
     * 回复 语音消息
     * @param $mainAttr
     * @return string
     */
    public static function voice($mainAttr)
    {
        $framework = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
</xml>";

        $resultStr = sprintf($framework, $mainAttr['fromUsername'], $mainAttr['toUsername'], $mainAttr['time'], 'voice', $mainAttr['mediaID']);
        return $resultStr;
    }

    /**
     * 回复 视频消息
     * @param $mainAttr
     * @return string
     */
    public static function video($mainAttr)
    {
        $framework = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>

<Video>
<MediaId><![CDATA[%s]]></MediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video> 

</xml>";

        $resultStr = sprintf($framework, $mainAttr['fromUsername'], $mainAttr['toUsername'], $mainAttr['time'], 'video', $mainAttr['mediaID'], $mainAttr['title'], $mainAttr['description']);
        return $resultStr;
    }

    /**
     * 回复 音乐消息
     * @param $mainAttr
     * @return string
     */
    public static function music($mainAttr)
    {
        $framework = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
</Music>
</xml>";
        $resultStr = sprintf($framework, $mainAttr['fromUsername'], $mainAttr['toUsername'], $mainAttr['time'], 'music', $mainAttr['title'], $mainAttr['description'], $mainAttr['musicURL'], $mainAttr['hQMusicUrl'], $mainAttr['thumbMediaId']);
        return $resultStr;
    }

    /**
     * 回复 图文消息
     * @param $mainAttr
     * @return string
     */
    public static function news($mainAttr)
    {
        $framework = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>

<Articles>
%s
</Articles>
</xml> ";

        $num = count($mainAttr['articleCount']);
        $itemStr = '';
        foreach ($mainAttr['articleCount'] as $article) {
            $item = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
            $itemStr .= sprintf($item, $article['title'], $article['description'], $article['picUrl'], $article['url']);
        }

        $resultStr = sprintf($framework, $mainAttr['fromUsername'], $mainAttr['toUsername'], $mainAttr['time'], 'news', $num, $itemStr);
        return $resultStr;
    }
}